﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Relocation.Com.Tools
{
    /// <summary>Class <c>Validator</c> 提供通用的数据字段验证方法</summary>
    public static class Validator
    {
        /// <summary>验证必填值</summary>
        /// <param><c>source</c> 这是需要验证的原始数据.</param>
        /// <returns>验证通过返回：True，否则返回：false..</returns>
        public static bool Required(string source)
        {            
            return !(String.IsNullOrEmpty(source));
        }

        /// <summary>验证长度</summary>
        /// <param><c>source</c> 这是需要验证的原始数据.</param>
        /// <returns>验证通过返回：True，否则返回：false..</returns>
        public static bool length(string source, int minlength, int maxlength)
        {
            int Length=System.Text.Encoding.Default.GetByteCount(source);
            return Length >= minlength && Length <= maxlength;
                
        }

        /// <summary>验证数字型</summary>
        /// <param><c>source</c> 这是需要验证的原始数据.</param>
        /// <returns>验证通过返回：True，否则返回：false.</returns>
        public static bool Digit(string source)
        {           
            //foreach (char a in source)
            //{
            //    if ((a > '9' || a < '0')&&a!='.') return false;
            //}

            return (Regex.IsMatch(source, @"^([0-9]{1,7})+(\.[0-9]{0,2})?$")||String.IsNullOrEmpty(source));
            //return true;
        }

        /// <summary>
        /// 验证整数
        /// </summary>
        /// <param name="source">需要验证的字符串</param>
        /// <returns>TRUE or false</returns>
        /// <remarks></remarks>
        public static bool Num(string source)
        {
            return (Regex.IsMatch(source, @"^(0|[1-9]?[1-9]\d*)$") || String.IsNullOrEmpty(source));            
        }

        /// <summary>验证日期型</summary>
        /// <param><c>source</c> 这是需要验证的原始数据.</param>
        /// <returns>验证通过返回：True，否则返回：false.</returns>
        public static bool Datetime(string source)
        {
            System.DateTime a;
            return System.DateTime.TryParse(source,out a);
        }

        /// <summary>根据规则表达式验证</summary>
        /// <param><c>source</c> 这是需要验证的原始数据.</param>
        /// <param><c>regex</c> 验证用的规则表达式.</param>
        /// <returns>验证通过返回：True，否则返回：false.</returns>
        public static bool rule(string source, string regex)
        {
            return Regex.IsMatch(source, regex);
        }




        #region 身份证格式验证

        /// <summary>
        /// 验证身份证号（15位或18位）
        /// </summary>
        /// <param name="cid">身份证号</param>
        /// <returns>是否合法</returns>
        public static bool CheckCid(string cid)
        {
            if (cid.Equals(string.Empty))   //只对格式验证，不对是否为空验证
                return true;
            if (rule(cid, "^\\d+x?$"))
            {
                if (cid.Length == 15)
                {
                    return Idcard15(cid);
                }
                else if (cid.Length == 18)
                {
                    return Idcard18(cid);
                }
               
                else return false;  //长度不对
            }
            else
            {
                return false;
            }
        }
        
        public static bool Idcard18(string source)
        {
            string year = source.Substring(6, 4);
            string month = source.Substring(10, 2);
            string day = source.Substring(12, 2);
            string date = year + "-" + month + "-" + day;
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Idcard15(string source)
        {
            string year = source.Substring(6, 2);
            string month = source.Substring(8, 2);
            string day = source.Substring(10, 2);
            string date = year + "-" + month + "-" + day;
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }


}
